package orders

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"mall_admin_api/biz/dal"
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin_orders_log"
	"mall_admin_api/biz/pack"
)

func (o *Orders) SyncLogOrdersLog(ctx context.Context, orderID int64, remark string) {
	go func() {
		defer func() {
			if err := recover(); err != nil {
				hlog.CtxErrorf(ctx, "panic %s", err)
			}
		}()
		admin := o.commonInfo.GetAdministratorInfo()
		data := &model.OrdersLog{
			OrderID:   orderID,
			AdminID:   admin.ID,
			Remark:    remark,
			CreatedBy: admin.Nickname,
			BizID:     o.commonInfo.GetBizInfo().ID,
		}
		if _, err := o.dao.OrdersLogDAO.CreateOrdersLog(ctx, data); err != nil {
			hlog.CtxErrorf(ctx, "dal.CreateOrdersLog err: %v", err)
		}

	}()
}

func (o *Orders) GetOrderLogList(ctx context.Context, req *admin_orders_log.GetOrderLogListRequest) ([]*admin_orders_log.OrderLog, error) {
	list, err := o.dao.OrdersLogDAO.GetOrdersLogList(ctx, []gen.Condition{dal.OrdersLog.OrderID.Eq(req.GetOrderID())}, 0, 1000)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAllOrdersLogList err: %v", err)
		return nil, err
	}
	return pack.GetOrderLogList(list), nil
}
